package com.group.service.impl;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.group.configurations.AlipayConfig;
import com.group.mapper.OrderMapper;
import com.group.service.LoginService;
import com.group.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.entity.Good;
import pojo.entity.Order;
import pojo.entity.OrderGood;
import pojo.qo.GoodSales;
import pojo.qo.ShopOrder;
import pojo.qo.UserOrder;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LoginService loginService;

    @Override
    public int updateOrderStatus(Order order) {
        return orderMapper.updateOrder(order);
    }

    @Override
    public int deleteOrder(Order order) {
        return orderMapper.deleteOrder(order);
    }

    @Override
    public List<Order> searchOrderByStatus(Order order) {
        return orderMapper.getOrderByStatus(order);
    }

    @Override
    public List<Order> searchOrderByUserId(Order order) {
        return orderMapper.getOrderByUserId(order);
    }

    @Override
    public List<Order> getSomeOrder(long userId, int page) {
//        PageHelper.startPage(page,3);
        return orderMapper.getSomeOrder(userId);
    }

    @Override
    public List<ShopOrder> searchOrderByOrderNumber(Order order) {
        return orderMapper.getOrderByOrderNumber(order);
    }

    @Override
    public int addOrder(Order order) {
        //增加订单里的商品集合
        List<Good> goods = order.getGood();
        //销量
        OrderGood orderGood = new OrderGood();
        //日志商品计算量
        AtomicInteger goodSub = new AtomicInteger();
        //遍历商品集合获取值
        goods.forEach(good -> {
            //下单商品量增加
            goodSub.set(goodSub.get() + good.getGoodCount());
            //封装成销量实体类
            orderGood.setGoodCount(good.getGoodCount());
            orderGood.setOrderNumber(order.getOrderNumber());
            orderGood.setGoodId(good.getGoodId());
            orderMapper.addOrderGoods(orderGood);
            //写入销量表
            //查询是否已经存在此商品的销量表
            GoodSales goodSales = orderMapper.hasGoodSales(good.getGoodId());
            if (null == goodSales) {
                //不存在
                //新增此商品销量数据
                GoodSales addGoodSales = new GoodSales();
                addGoodSales.setGoodId(good.getGoodId());
                addGoodSales.setSalesVolume((long) good.getGoodCount());
                System.out.println("新增销量:" + addGoodSales);
                int i = orderMapper.addGoodSales(addGoodSales);
                System.out.println("新增商品销量数据:" + i);
            } else {
                //已存在此商品记录
                //修改计数数据
                goodSales.setGoodId(good.getGoodId());
                goodSales.setSalesVolume(goodSales.getSalesVolume() + good.getGoodCount());
                int i = orderMapper.updateGoodSales(goodSales);
                System.out.println("更新销量数量:" + i);
            }

        });

        //写入日志
        System.out.println("写入日志:" + goodSub);
        loginService.logAction(order.getUserId(), goodSub.get());


        return orderMapper.addOrder(order);
    }

    @Override
    public List<Order> searchAllOrderByOrderNumber(Order order) {
        return orderMapper.selectOrderWithStatus(order);
    }

    @Override
    public Order selectOrderByOrderId(Long orderNumber) {
        return orderMapper.selectOrderByOrderId(orderNumber);
    }

    @Override
    public List<ShopOrder> searchOrderByUserId2(Order order) {
        return orderMapper.selectTest(order.getUserId());
    }
}
